package test.my.com.myapplicationtest.fragment_tab_host;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import butterknife.Bind;
import butterknife.ButterKnife;
import test.my.com.myapplicationtest.R;

/**
 * Author : Cooper
 * Time : 2016/9/6  16:03
 * Description : 利用TabLayout实现底部tab
 */
public class TabLayoutActivity extends FragmentActivity {

    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;

    private String[] tabs = {"综合","新闻","体育"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_tablayout_layout);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;
                if (position == 0) {
                    fragment = new FristFragment();
                } else if(position == 1){
                    fragment = new SecondFragment();
                } else if(position == 2){
                    fragment = new ThridFragment();
                }
                return fragment;
            }

            @Override
            public int getCount() {
                return tabs.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });

        tabLayout.setupWithViewPager(viewPager);

        TabLayout.Tab oneTab = tabLayout.getTabAt(0);
        TabLayout.Tab TwoTab = tabLayout.getTabAt(1);
        TabLayout.Tab ThridTab = tabLayout.getTabAt(2);

        oneTab.setIcon(R.drawable.duigou);
        TwoTab.setIcon(R.drawable.duigou);
        ThridTab.setIcon(R.drawable.duigou);

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
